require(['grid', 'topBar', 'layout', 'select2'], function () {
    var $grid = $("#grid"), $top = $('#top'), reloadGridData, lineIds, carIds, drvIds, locationDetailInfoDialog;

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '当前状态',
                    name: 'offlineHours',
                    width: 80,
                    type: 'select',
                    data: [
                        {text: '全部', value: ''},
                        {text: '在线', value: '在线'},
                        {text: '离线', value: '离线'},
                        {text: '从未在线', value: '从未在线'}
                    ]
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                        reloadGridData();
                    }
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                                exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                                exportFile('Pdf');
                            }
                        }
                    ]
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/vehicleOnLine/searchVehicleOnLineStatus.htm",
            colModel: [
                {name: "carId", label: "carId", key: true, hidden: true},
                {name: "loLgt", label: "loLgt", hidden: true},
                {name: "loLtt", label: "loLtt", hidden: true},
                {name: "organName", label: "所属机构"},
                {name: "lineName", label: "运营线路"},
                {name: "licPltNo", label: "车牌号"},
                {name: "eqmtCd", label: "设备ID"},
                {name: "simCardno", label: "SIM卡号"},
                {name: "lastUpdateDate", label: "最后版本升级日期"},
                {name: "vrsnName", label: "最后升级版本"},
                {
                    name: "location",
                    label: "最后在线位置",
                    sortable: false,
                    align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        var address;
                        if (rowObject.loLgt <= '0' && rowObject.loLtt <= '0') {
                            address = "未知";
                        } else {
                            address = "<b><a href='javascript:void(0);' onclick='queryLocation(\"" + rowObject.licPltNo + "\",\"" + rowObject.drvName + "\",\"" + rowObject.loLgt + "\",\"" + rowObject.loLtt + "\",\"" + rowObject.loDrc + "\"" + ")' style='color: blue;'>"
                            + "查看详细位置</a></b>";
                        }
                        return address;
                    },
                    unformat: function (value) {
                        return value;
                    },
                    width: '240',
                    fixed: false
                },
                {name: "onlineSt", label: "当前状态" },
                {name: "rcrdTime", label: "离线时间"},
                {name: "offlineHours", label: "离线时长(小时)", sortable: false, align: "right", width: '120', fixed: true}],
            gridComplete: function () {
                var rowIds = $grid.jqGrid('getDataIDs');
                var cssprop = "";
                var cssprop1 = "";
                for (var i = 0; i < rowIds.length; i++) {
                    var rowId = rowIds[i];
                    var rowObject = $grid.jqGrid('getRowData', rowId);
                    if (rowObject.rcrdTime == '从未在线') {
                        cssprop = {color: "#FF100B"};
                    } else {
                        cssprop = {color: "#000"};
                    }
                    $grid.jqGrid("setCell", rowId, "rcrdTime", rowObject.rcrdTime, cssprop);

                    if (rowObject.offlineHours > 24) {
                        cssprop1 = {color: "#FF100B"};
                    } else {
                        cssprop1 = {color: "#000"};
                    }
                    $grid.jqGrid("setCell", rowId, "offlineHours", rowObject.offlineHours, cssprop1);
                }
                var cells = $grid.find('td[aria-describedby="vehicleOLStatusList_location"]');
                $.each(cells, function (i, obj) {
                    var title = $grid.jqGrid('getCell', rowIds[i], 1) + "," + $grid.jqGrid('getCell', rowIds[i], 2);
                    $(obj).attr('title', title);
                });
            }
        });
    }

    //-- 显示 详细位置信息
    queryLocation = function (licPltNo, drvName, beginLoLgt, beginLoLtt, beginLoDrc, type, spedBeginTime, spedEndTime) {
        if (drvName == "undefined") {
            drvName = "未知";
        }
        var data = new Array();// 在这里构造车辆数据的数组
        var obj = new Object();//车辆数据
        obj.licPltNo = licPltNo;//车牌号
        obj.driverName = drvName;//驾驶员
        obj.loLgt = beginLoLgt;//经度
        obj.loLtt = beginLoLtt;//纬度
        obj.direction = beginLoDrc;//方向
        obj.type = type;//异常类型 字符串表示
        obj.beginTime = spedBeginTime;//开始时间
        obj.endTime = spedEndTime;//结束时间
        data.push(obj);
        openLocationDetailInfoDialog("详细位置", data); //弹出的标题和数据
    };

    function openLocationDetailInfoDialog(title, data) {
        if (!locationDetailInfoDialog) {
            locationDetailInfoDialog = $('<div>').dialog({
                title: title,
                url: './common/map/mapLocation.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        locationDetailInfoDialog.dialog('open');
        locationDetailInfoDialog.dialog('exec', 'showMapLocation', data);
    }


    function exportFile(suffix) {
        if ($grid.getGridParam("reccount") > 0) {
            var url = "/vehicleOnLine/exportVehicleOLStatus" + suffix + ".htm";
            var type = commonFun.getLeftTreeCurrentTreeType();
            var orgIds = commonFun.getLeftTreeCheckedNodes(type);
            checkIds();
            if (!orgIds) {
                $.messager.alert("提示", "请选择查询车辆/机构/驾驶员。", "info");
            } else {
                var $postData = $("#top").parent().getValues();
                $postData.type = type;
                $postData.orgIds = orgIds.toString();
                $postData.carIds = carIds;
                $postData.lineIds = lineIds;
                $.exportFile(url, $postData);
            }

        } else {
            $.messager.alert("提示", "请先查询数据。", "info");
        }
    }

    //查询grid
    reloadGridData = function () {
        var $postData = $("#top").parent().getValues();
        // 左侧树tab选中的类型
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        checkIds();
        if (!orgIds) {
            $.messager.alert("提示", "请选择查询车辆/机构/驾驶员！", "info");
        } else {
            $postData.type = type;
            $postData.orgIds = orgIds.toString();
            $postData.carIds = carIds;
            $postData.lineIds = lineIds;
            $('#grid').grid('setGridParam', {
                datatype: "json",
                postData: $postData
            }).trigger("reloadGrid");
        }
    };

    //获取左侧选择的节点信息
    function checkIds() {
        ///清空节点参数
        lineIds = "";
        carIds = "";
        drvIds = "";
        var includeIds = false;//是否选择了节点
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.CAR) {//是否包含carIds
            carIds = nodes.CAR.toString();
            includeIds = true;
        }
        if (nodes.LINE) {//是否包含lineIds
            lineIds = nodes.LINE.toString();
            includeIds = true;
        }
        return includeIds;
    }


    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});
